﻿using HCQ2_Common;
using HCQ2_Common.Constant;
using HCQ2_Model;
using HCQ2_Model.ViewModel.VR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace HCQ2UI_Logic.DocumentController
{
    /// <summary>
    /// 购物车    
    /// </summary>
    public class ShoppingCartController:BaseLogic
    {

        /// <summary>
        /// 购物车管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 新版购物车；20191018需求
        /// </summary>
        /// <returns></returns>
        public ActionResult Index2(string fileids)
        {
            var fileInfos = operateContext.bllSession.T_VRShoppingCart.GetSelectFilePayInfo(fileids);

            foreach (var fileInfo in fileInfos)
            {
                if (!string.IsNullOrEmpty(fileInfo.FileImgUrl)) {
                    fileInfo.FileImgUrl = Request.ApplicationPath + fileInfo.FileImgUrl.Replace("~","");
                }
            }

            ViewBag.FilePayInfo = fileInfos;

            ViewBag.UserId = operateContext.Usr.user_guid;
            ViewBag.ApiUrl = ConfigurationHelper.AppSetting(SystemConstant.APIURL_ROOT_NAME);
            return View();
        }

        /// <summary>
        /// 用户的购物车
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ActionResult UserShoppingCart(Nullable<int> userId)
        {
            if (userId != null)
            {
                UserShoppingCart cart = operateContext.bllSession.T_VRShoppingCart.GetUserShoppingCart(userId.Value);
                ViewBag.cart = cart;
                ViewBag.ApiUrl = ConfigurationHelper.AppSetting(SystemConstant.APIURL_ROOT_NAME);
            }
            return View();
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="userName">搜索的用户</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UserListData(string userName)
        {
            Expression<Func<T_User, bool>> whereLambda = w=>w.user_id>0;
            if (!string.IsNullOrEmpty(userName))
            {
                whereLambda = w => w.user_name.Contains(userName);
            }
            List<T_User> userList = operateContext.bllSession.T_User.Select(whereLambda);

            List<UserList> userListResult = userList.MapToList<UserList>();
            return operateContext.ok(userListResult);
        }

    }
}
